/**
 *@Description 智能监管-清单抽查
 */
var billCtrl  = new XFApp.client();
XFApp.client.prototype.bill = {
	successCode : "10000",
	page : null,
	hgPage : null,
	urlItems : {
		queryByPageURL : basePath+"/billMonitor/queryPageForCheck",
		queryHgGoodsByPageURL : basePath+"/billMonitor/queryHgGPage",
		setStatusURL : basePath+"/billMonitor/setCheckStatus"
	},
	queryPage : function(){
		_this = this;
		if(!!_this.page){
			_this.page.page("remote",0,$('#searchForm').serialize().replace(/\+/g, "%20"));
		}else{
		_this.page = $("#page").page({
			pageSize : 10,
			remote: {
				url: _this.urlItems.queryByPageURL,
		        params: $('#searchForm').serialize().replace(/\+/g, "%20"), 
		        success: function (data, pageIndex) {
		        	if(data.code==_this.successCode){
		        		var pageResult = data.result;
		        		if(null == data 
								|| null == data.result 
									|| null == data.result.itemList
										|| data.result.itemList.length < 1){
		        			$("#page_grid").html('<tr><td align="center" colspan="99">暂无数据</td></tr>');
		        		}else{
		        			for(var index in pageResult.itemList){
		        				var element = pageResult.itemList[index];
		        				element["seqNum"]=parseInt(pageResult.pageSize)*(parseInt(pageResult.pageNo)-1)+parseInt(index)+1;
		        			}
		        			$("#page_grid").html($("#dataTmp").render(pageResult.itemList));
		        		}
		        		if(!$("#checkStatus").val() || $("#checkStatus").val()=="1"){
		        			$("#opt_btn_group").show();
		        		}else{
		        			$("#opt_btn_group").hide();
		        		}
		        	}else{
		        		window.webDialogUtil.alert("提示","操作失败");
		        	}
		        },
		        error : function(){
		        	window.webDialogUtil.alert("提示","操作失败");
		        }
			},
			firstBtnText : '首页',
			lastBtnText : '尾页',
			prevBtnText : '上一页',
			nextBtnText : '下一页',
			showInfo : true,
			showJump : true,
			jumpBtnText : '跳转',
			showPageSizes : true,
			infoFormat : '共{pageCount}页,{total}项'
		});
		}
		$("#check_all").prop("checked","");
	},
	
	schgCheckStatus : function(id,status,type){
		var ids = [];
		ids.push(id);
		
		var tipStr = "";
		switch(status){
		case 2:
			tipStr = "合格";
			break;
			
		case 3:
			tipStr = "不合格";
			break;
		}
		var params = {
			ids:ids,
			checkStatus:status
		};
		if(!!type){
			params['statusType']=type;
		}
		webDialogUtil.simpleConfrimAlert("是否确认清单为"+tipStr+"？", function(){
			$.ajax({
				url : _this.urlItems.setStatusURL,
				dataType : 'json',
				traditional:true,
				data  : params,
				type : 'post',
				success : function(data) {
					if(data.code!=10000){
						webDialogUtil.simpleAlert(data.msg, "温馨提示");
					}else{
						webDialogUtil.simpleAlert("处理成功", "温馨提示");
						//_this.queryPage();
						$("#checkStatus_"+id).html(tipStr);
						$(".opt_"+id).hide();
					}
				},
				error:function(){
					webDialogUtil.simpleAlert("处理异常请联系开发人员", "温馨提示");
				}
			
			});
		}, null, "温馨提示");
	},
	
	chgCheckStatus : function(status,type){
		
		var ids = [];
		var isIlleal = false;
		$("[id^=checkbox_]").each(function(){
			if(!!$(this).prop("checked")){
				ids.push($(this).val());
				if($(this).attr("data-status")!='1'){
					isIlleal = true;
				}
			}
		});
		
		if(isIlleal){
			webDialogUtil.simpleAlert("请选择待检查的清单", "温馨提示");
			return;
		}
		if(ids.length == 0){
			webDialogUtil.simpleAlert("请选择需要操作的选项", "温馨提示");
			return;
		}
		var tipStr = "";
		switch(status){
		case 2:
			tipStr = "合格";
			break;
			
		case 3:
			tipStr = "不合格";
			break;
		}
		var params = {
			ids:ids,
			checkStatus:status
		};
		if(!!type){
			params['statusType']=type;
		}
		webDialogUtil.simpleConfrimAlert("是否确认选中清单为"+tipStr+"？", function(){
			$.ajax({
				url : _this.urlItems.setStatusURL,
				dataType : 'json',
				traditional:true,
				data  : params,
				type : 'post',
				success : function(data) {
					if(data.code!=10000){
						webDialogUtil.simpleAlert(data.msg, "温馨提示");
					}else{
						webDialogUtil.simpleAlert("处理成功", "温馨提示");
						_this.queryPage();
					}
				},
				error:function(){
					webDialogUtil.simpleAlert("处理异常请联系开发人员", "温馨提示");
				}
			
			});
		}, null, "温馨提示");
	},
	clk_check : function(){
		if(!!$("#check_all").prop("checked")){
			$("[id^=checkbox_]").prop("checked","checked");
		}else{
			$("[id^=checkbox_]").prop("checked","");
		}
	},
	queryGoodsPage : function(){
		_this = this;
		var params = {
			code : $("#codeFilter").val(),
			nameFuzzy : $("#nameFilter").val()
		}
		if(!!_this.hgPage){
			_this.hgPage.page("remote",0,params);
		}else{
			_this.hgPage = $("#hgPage").page({
				pageSize : 5,
				remote: {
					url: _this.urlItems.queryHgGoodsByPageURL,
			        params: params, 
			        success: function (data, pageIndex) {
			        	if(data.code==_this.successCode){
			        		var pageResult = data.result;
			        		_this.hgGoods = pageResult.itemList;
			        		if(pageResult == null || pageResult.itemList == null || pageResult.itemList.length==0){
			        			$("#data_grid").html('<tr><td align="center" colspan="99">暂无数据</td></tr>');
			        		}else{
			        			$("#data_grid").html($("#goods_dataTmp").render(pageResult.itemList));
			        		}
			        	}else{
			        		webDialogUtil.simpleAlert(data.msg, "温馨提示"); 
			        	}
			        },
			        error : function(){
			        	webDialogUtil.simpleAlert("数据查询异常，请联系开发人员", "温馨提示"); 
			        }
				},
				firstBtnText : '首页',
				lastBtnText : '尾页',
				prevBtnText : '上一页',
				nextBtnText : '下一页',
				showInfo : true,
				showJump : true,
				jumpBtnText : '跳转',
				showPageSizes : true,
				infoFormat : '共{pageCount}页'
			});
		}
	},
	selGcode : function(selGcode){
		$("#gcode").val(selGcode);
		$("#choose_goods_modal").modal('hide'); 
	},
	dataFormat : function(timeLong){
		if(null == timeLong
				|| "" == timeLong){
			return "";
		}
		return new Date(timeLong*1000).format("yyyy-MM-dd hh:mm:ss")
	},
	event : function(){
		_this = this;
		$("#check_all").on("click",function(){
			_this.clk_check();
		});
		$("#gcode_choose").on("click",function(){
			$("#choose_goods_modal").modal();
			_this.queryGoodsPage();
		});
		$("#goods_search").on("click",function(){
			_this.queryGoodsPage();
		});
		$("#goods_reset").on("click",function(){
			$("#codeFilter").val("");
			$("#nameFilter").val("");
		});
		$("#searchButton").on("click",function(){
			_this.queryPage();
		});
		$("#checkStatus").val($("#checkStatus_sel").val());
		$("#checkStatus_sel").on("change",function(){
			$("#checkStatus").val($(this).val());
			billCtrl.bill.queryPage();
		});
		billCtrl.bill.queryPage();
	},
};
$.views.converters({
	ftime : function(value) {
		return billCtrl.bill.dataFormat(value) ;
	},
	fdkstatus : function(value){
		return dksCtrl.dockerstatus.get(value);
	}
});
$(function(){
	dictCtrl.dictionary.load("billDocketStatus","docketStatusHidden");
	billCtrl.bill.event();
});